package iewShopApp.namespace;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class TapActivity extends Activity {

	public GlobalVars Constants;
	public RadioButton imperialSelected;
	private ImageButton HomeButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tap_main);
		Constants = new GlobalVars();
		RadioButton imperialSelected = (RadioButton) findViewById(R.id.tapImperialButton);
		imperialSelected.setChecked(true);
		Spinner spinner = (Spinner) findViewById(R.id.spinner_tap);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.tap_string, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new MyOnItemSelectedListenerT1());
		noFocus();
		Constants.tapsfm = 0;
		Spinner tapMaterialSpinner = (Spinner) findViewById(R.id.spinner_tap_material);
		tapMaterialSpinner.setSelection(4);
		tapMaterialSpinner
				.setOnItemSelectedListener(new MyOnItemSelectedListener2());
		final EditText userMachine = (EditText) findViewById(R.id.tapMachineRPM);
		userMachine.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button

				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					String strValue = userMachine.getText().toString();
					Constants.maxRPM = Integer.parseInt(strValue);
					setSpeed();
					setFeed();
					// Force EditText from gaining focus all the bloody time
					userMachine.clearFocus();
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(userMachine.getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
					return true;
				}
				return false;
			}

		});

		setSpeed();
		setFeed();
	}

	// if Imperial Selected
	public class MyOnItemSelectedListenerT1 implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			TextView tapStringDisplay = (TextView) findViewById(R.id.tapString);
			TextView tapDoubleDisplay = (TextView) findViewById(R.id.tapDouble);
			TextView clearanceStringDisplay = (TextView) findViewById(R.id.clearanceString);
			TextView clearanceValDisplay = (TextView) findViewById(R.id.clearanceDouble);
			TextView peckDisplay = (TextView) findViewById(R.id.tapMaxPeck);
			// TextView spindleDisplay = (TextView)
			// findViewById(R.id.textSpindleSpeed);

			String selectedString = getResources().getStringArray(
					R.array.tap_drill)[parent.getSelectedItemPosition()];
			String selectedVal = getResources().getStringArray(
					R.array.tap_drill_double)[parent.getSelectedItemPosition()];
			String selectedClearanceVal = getResources().getStringArray(
					R.array.clearance_hole_double)[parent
					.getSelectedItemPosition()];
			String selectedClearanceString = getResources().getStringArray(
					R.array.clearance_hole)[parent.getSelectedItemPosition()];

			tapStringDisplay.setText(selectedString);
			tapDoubleDisplay.setText(selectedVal);
			clearanceStringDisplay.setText(selectedClearanceString);
			clearanceValDisplay.setText(selectedClearanceVal);
			noFocus();

			Constants.drillSize = Double.parseDouble(selectedVal);
			peckDisplay.setText(String.valueOf(Math
					.round(1000 * (Constants.drillSize) / 4) / 1000.));
			setSpeed();
			setFeed();
		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

	public class MyOnItemSelectedListener2 implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {

			String sfmString = getResources().getStringArray(
					R.array.material_array_sfm)[parent
					.getSelectedItemPosition()];
			int sfmInt = Integer.parseInt(sfmString);

			// Constants.drillMaterial = materialInt;
			Constants.tapsfm = sfmInt;
			setSpeed();
			setFeed();

		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

	// if Metric Selected
	public class MyOnItemSelectedListenerT2 implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			TextView tapStringDisplay = (TextView) findViewById(R.id.tapString);
			TextView tapDoubleDisplay = (TextView) findViewById(R.id.tapDouble);
			TextView clearanceStringDisplay = (TextView) findViewById(R.id.clearanceString);
			TextView clearanceValDisplay = (TextView) findViewById(R.id.clearanceDouble);
			TextView peckDisplay = (TextView) findViewById(R.id.tapMaxPeck);
			// TextView spindleDisplay = (TextView)
			// findViewById(R.id.textSpindleSpeed);

			String selectedVal = getResources().getStringArray(
					R.array.Metric_tap_double)[parent.getSelectedItemPosition()];
			String selectedString = getResources().getStringArray(
					R.array.Metric_tap_string)[parent.getSelectedItemPosition()];
			String selectedClearanceVal = getResources().getStringArray(
					R.array.metric_clearance_double)[parent
					.getSelectedItemPosition()];
			String selectedClearanceString = getResources().getStringArray(
					R.array.metric_clearance_string)[parent
					.getSelectedItemPosition()];

			tapStringDisplay.setText(String.valueOf(selectedString));
			tapDoubleDisplay.setText(selectedVal);
			clearanceStringDisplay.setText(selectedClearanceString);
			clearanceValDisplay.setText(selectedClearanceVal);
			noFocus();

			Constants.drillSize = Double.parseDouble(selectedVal);
			peckDisplay.setText(String.valueOf(Math
					.round(1000 * (Constants.drillSize) / 4) / 1000.));
			setSpeed();
			setFeed();
		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

	// if Pipe Selected
	public class MyOnItemSelectedListenerT3 implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			TextView tapStringDisplay = (TextView) findViewById(R.id.tapString);
			TextView tapDoubleDisplay = (TextView) findViewById(R.id.tapDouble);
			TextView clearanceStringDisplay = (TextView) findViewById(R.id.clearanceString);
			TextView clearanceValDisplay = (TextView) findViewById(R.id.clearanceDouble);
			TextView peckDisplay = (TextView) findViewById(R.id.tapMaxPeck);
			// TextView spindleDisplay = (TextView)
			// findViewById(R.id.textSpindleSpeed);

			String selectedVal = getResources().getStringArray(
					R.array.pipe_tap_double)[parent.getSelectedItemPosition()];
			String selectedString = getResources().getStringArray(
					R.array.pipe_tap_string)[parent.getSelectedItemPosition()];
			String selectedClearanceVal = getResources().getStringArray(
					R.array.pipe_clearance)[parent.getSelectedItemPosition()];
			String selectedClearanceString = getResources().getStringArray(
					R.array.pipe_clearance)[parent.getSelectedItemPosition()];

			tapStringDisplay.setText(String.valueOf(selectedVal));
			tapDoubleDisplay.setText(selectedString);
			clearanceStringDisplay.setText(selectedClearanceString);
			clearanceValDisplay.setText(selectedClearanceVal);
			noFocus();

			Constants.drillSize = Double.parseDouble(selectedVal);
			peckDisplay.setText(String.valueOf(Math
					.round(1000 * (Constants.drillSize) / 4) / 1000.));
			setSpeed();
			setFeed();
		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

	public void onRadioTapClicked(View v) {
		// Perform action on clicks

		TextView tapStringDisplay = (TextView) findViewById(R.id.tapString);
		TextView tapDoubleDisplay = (TextView) findViewById(R.id.tapDouble);

		// TextView spindleDisplay = (TextView)
		// findViewById(R.id.textSpindleSpeed);
		RadioButton imperialSelected = (RadioButton) findViewById(R.id.tapImperialButton);
		RadioButton metricSelected = (RadioButton) findViewById(R.id.TapMetricButton);
		RadioButton pipeSelected = (RadioButton) findViewById(R.id.tapPipeButton);

		RadioButton rb = (RadioButton) v;
		noFocus();
		if (rb.getText().equals("Imperial")) {
			metricSelected.setChecked(false);
			pipeSelected.setChecked(false);
			Spinner spinner = (Spinner) findViewById(R.id.spinner_tap);
			ArrayAdapter<CharSequence> adapter = ArrayAdapter
					.createFromResource(this, R.array.tap_string,
							android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner.setAdapter(adapter);
			spinner.setOnItemSelectedListener(new MyOnItemSelectedListenerT1());

		} else {
			if (rb.getText().equals("Metric")) {
				imperialSelected.setChecked(false);
				pipeSelected.setChecked(false);
				Spinner spinner = (Spinner) findViewById(R.id.spinner_tap);
				ArrayAdapter<CharSequence> adapter = ArrayAdapter
						.createFromResource(this, R.array.Metric_taps,
								android.R.layout.simple_spinner_item);
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				spinner.setAdapter(adapter);
				spinner.setOnItemSelectedListener(new MyOnItemSelectedListenerT2());

			}

			else {
				metricSelected.setChecked(false);
				imperialSelected.setChecked(false);
				Spinner spinner = (Spinner) findViewById(R.id.spinner_tap);
				ArrayAdapter<CharSequence> adapter = ArrayAdapter
						.createFromResource(this, R.array.pipe_tap,
								android.R.layout.simple_spinner_item);
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				spinner.setAdapter(adapter);
				spinner.setOnItemSelectedListener(new MyOnItemSelectedListenerT3());

			}
		}
		setSpeed();
		setFeed();
	}

	void noFocus() {
		TextView tapStringDisplay = (TextView) findViewById(R.id.tapString);
		TextView tapDoubleDisplay = (TextView) findViewById(R.id.tapDouble);
		TextView clearanceStringDisplay = (TextView) findViewById(R.id.clearanceString);
		TextView clearanceValDisplay = (TextView) findViewById(R.id.clearanceDouble);
		tapStringDisplay.clearFocus();
		tapDoubleDisplay.clearFocus();
		clearanceStringDisplay.clearFocus();
		clearanceValDisplay.clearFocus();
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(tapStringDisplay.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
		InputMethodManager imm2 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm2.hideSoftInputFromWindow(tapDoubleDisplay.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
		InputMethodManager imm3 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm3.hideSoftInputFromWindow(clearanceStringDisplay.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
		InputMethodManager imm4 = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm4.hideSoftInputFromWindow(clearanceValDisplay.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);

	}

	public void setSpeed() {
		TextView tapspindleDisplay = (TextView) findViewById(R.id.tapSpindleSpeed);
		Constants.drillRPM = Math.min(
				Math.round(Constants.tapsfm * 12
						/ ((Constants.drillSize) * Math.PI)), Constants.maxRPM);
		tapspindleDisplay.setText(String.valueOf((int) Constants.drillRPM));
	}

	public void setFeed() {
		TextView tapFeed = (TextView) findViewById(R.id.tapFeedRate);
		// double RPM = Math.round(Constants.drillsfm * 12
		// / (Double.parseDouble(Constants.drillSize) * Math.PI));

		tapFeed.setText(String.valueOf(Math.round(Constants.drillRPM * .01
				* (Constants.drillSize) / (.0625)) / 10.));
		// drillFeed.setText(String.valueOf(RPM));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.textMill:
			setContentView(R.layout.mill);
			addListenerOnButton7();
			break;
		case R.id.textDrill:
			finish();
			Intent myIntent1 = new Intent().setClass(this, DrillActivity.class);
			startActivity(myIntent1);
			// setContentView(R.layout.drill);

			// addListenerOnButton7();
			break;
		case R.id.textLathe:
			setContentView(R.layout.lathe);
			addListenerOnButton7();
			break;
		case R.id.textReam:
			setContentView(R.layout.ream);
			addListenerOnButton7();
			break;
		case R.id.textTap:
			finish();
			Intent myIntent2 = new Intent().setClass(this, TapActivity.class);
			startActivity(myIntent2);
			// setContentView(R.layout.tap);
			// addListenerOnButton7();
			break;
		case R.id.textLearning:
			// trial = new LearningActivity();
			// trial.update();
			setContentView(R.layout.learning);
			// defineLearningButtons();
			// addListenerOnButton();
			// addListenerOnButton2();
			// addListenerOnButton3();
			// addListenerOnButton4();
			// addListenerOnButton5();
			// addListenerOnButton6();
			addListenerOnButton7();
			break;

		}
		return true;
	}

	public void addListenerOnButton7() {
		HomeButton = (ImageButton) findViewById(R.id.HomeClick);
		HomeButton.setOnClickListener(new OnClickListener() {

			// @Override
			public void onClick(View arg0) {
				finish();

			}
		});
	}
}